package com.hertz.iris.view.matchalarm;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.actionbarsherlock.app.SherlockFragment;
import com.hertz.iris.R;
import com.hertz.iris.pojo.MatchAlarm;
import com.hertz.iris.ui.SeekArc;
import com.hertz.iris.util.BundleKeys;
import com.hertz.iris.util.Constants;

public class VolumeSelectorFrag extends SherlockFragment{

	@InjectView(R.id.volume_picker)
	SeekArc volumePicker;
	@InjectView(R.id.volume_values)
	TextView volumeValueText;
	
	private OnVolumeSelectListener myCallback;
	
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			myCallback = (OnVolumeSelectListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnVolumeSelectListener");
        }
	}
	
	@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) { 
		super.onCreateView(inflater, container, savedInstanceState);
        
		View v = inflater.inflate(R.layout.volume_selector_frag, container, false);
		ButterKnife.inject(this,v);
		volumePicker.setMax(Constants.MAX_VALUME);
		Bundle ars = getArguments();
		MatchAlarm ma = (MatchAlarm)ars.getSerializable(BundleKeys.MatchAlarmBundleKeys.MATCH_ALARM_KEY);
		byte volume = ma.getVolume();
		volumePicker.setProgress(volume);
		volumeValueText.setText(String.format(getResources().getString(R.string.volume), volume));
		myCallback.volumeSelectListener(volume);
		
		volumePicker.setOnSeekArcChangeListener(new SeekArc.OnSeekArcChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekArc seekArc) {	
			}		
			@Override
			public void onStartTrackingTouch(SeekArc seekArc) {
			}
			
			@Override
			public void onProgressChanged(SeekArc seekArc, int progress,
					boolean fromUser) {
				volumeValueText.setText(String.format(getResources().getString(R.string.volume), progress));
				myCallback.volumeSelectListener(Integer.valueOf(progress).byteValue());
			}
		});
        return v;
    } 
	
	public interface OnVolumeSelectListener{
		public void volumeSelectListener(byte volumeValue);
	}
	
}
